﻿// ====================================
// Created By Angie.D 2010.6
// ====================================

using System;
using System.Collections.Generic;
using System.Text;

using System.Reflection;
using System.Text.RegularExpressions;
using System.Xml.Serialization;

namespace WOWMultiPlay
{
    [XmlRootAttribute("mpKeyArray", IsNullable = false)]
    public class mpKeyArray
    {
        private string _KeyArray;
        public string KeyArray
        {
            get 
            {
                return _KeyArray;
            }
            set
            {
                _KeyArray = value;
                TranslatedKeyArray = TranslateKeyArray(_KeyArray);
            }
        }

        public mpKey[] TranslatedKeyArray;

        public mpKeyArray() { }

        public mpKeyArray(string KeyArray)
        {
            this.KeyArray = KeyArray;
        }

        // Format:
        // {VK_1} -> VK
        // {VK_1,DU} -> VK, DownUp
        // {VK_1,DU,1s} -> VK, DownUp, KeyPressInterval
        // {VK_1,DU,1s,100ms} -> VK, DownUp, KeyPressInterval, KeyDownUpInterval
        private mpKey[] TranslateKeyArray(string KeyArray)
        {
            string[] Keys = GetKeys(KeyArray);
            mpKey[] TranslateKeyArray = new mpKey[Keys.Length];

            for (int i = 0; i < Keys.Length; i++)
            {
                TranslateKeyArray[i] = new mpKey();
                string[] KeyArg = Keys[i].Split(new char[] { ',' });
                TranslateKeyArray[i].Key = GetVKInt(KeyArg[0]);

                if (KeyArg.Length == 1)
                {
                    TranslateKeyArray[i].KeyPressOption = KeyPressOption.KeyDownAndUp;
                }
                if (KeyArg.Length >= 2 && KeyArg.Length <= 3)
                {
                    switch (KeyArg[1])
                    {
                        case "D":
                            TranslateKeyArray[i].KeyPressOption = KeyPressOption.KeyDown;
                            break;
                        case "DU":
                            TranslateKeyArray[i].KeyPressOption = KeyPressOption.KeyDownAndUp;
                            break;
                        default:
                            throw new Exception("Invalid Key Array.");
                    }
                }
                if (KeyArg.Length >= 3 && KeyArg.Length <= 4)
                {
                    TranslateKeyArray[i].KeyPressInterval = new mpTime(KeyArg[2]);
                }
                if (KeyArg.Length == 4)
                {
                    TranslateKeyArray[i].KeyDownUpInterval = new mpTime(KeyArg[3]);
                }
                if (KeyArg.Length > 4)
                {
                    throw new Exception("Invalid Key Array.");
                }
            }

            return TranslateKeyArray;
        }

        private string[] GetKeys(string KeysArray)
        {
            Regex KeysRegex = new Regex("{((?:.|\n)*?)}");
            MatchCollection KeysQueryMatches = KeysRegex.Matches(KeysArray);

            if (KeysQueryMatches.Count > 0)
            {
                string[] KeyArray = new string[KeysQueryMatches.Count];

                for (int i = 0; i < KeysQueryMatches.Count; i++)
                {
                    KeyArray[i] = KeysQueryMatches[i].Groups[1].Value;
                }

                return KeyArray;
            }
            else
            {
                return new string[0];
            }
        }

        private int GetVKInt(string VK)
        {
            Assembly Asm = Assembly.GetExecutingAssembly();
            Type TypeWin32API = Asm.GetType("WOWMultiPlay.WindowsAPI");
            Type TypeVK = TypeWin32API.GetNestedType("VirtualKey");

            FieldInfo FI = TypeVK.GetField(VK);
            return (int)FI.GetValue(null);
        }
    }

    [XmlRootAttribute("mpKey", IsNullable = false)]
    public class mpKey
    {
        public int Key;
        public KeyPressOption KeyPressOption;
        public mpTime KeyPressInterval = Settings.UserSettings.Interval_KeyPress;
        public mpTime KeyDownUpInterval = Settings.UserSettings.Interval_KeyDown_Up;

        public mpKey() { }

        public mpKey(int Key, KeyPressOption KeyPressOption)
        {
            this.Key = Key;
            this.KeyPressOption = KeyPressOption;
        }

        public mpKey(int Key, KeyPressOption KeyPressOption, mpTime KeyPressInterval)
        {
            this.Key = Key;
            this.KeyPressOption = KeyPressOption;
            this.KeyPressInterval = KeyPressInterval;
        }

        public mpKey(int Key, KeyPressOption KeyPressOption, mpTime KeyPressInterval, mpTime KeyDownUpInterval)
        {
            this.Key = Key;
            this.KeyPressOption = KeyPressOption;
            this.KeyPressInterval = KeyPressInterval;
            this.KeyDownUpInterval = KeyDownUpInterval;
        }
    }

    public enum KeyPressOption
    {
        KeyDown,
        KeyDownAndUp,
    }
}